package Stages

import Actors.Mario
import Listeners.TouchState
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.maps.objects.RectangleMapObject
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import copy
import flipX
import setOnTouchListener
import stageFlag

/**
 * 游戏舞台
 */
class GameStage(textureAtlas: TextureAtlas) : Stage()
{

    // 金币
    private val goldTexture by lazy { textureAtlas.findRegion("glod") }
    // 左边按钮
    private val leftBtn by lazy {
        val normal = TextureRegionDrawable(textureAtlas.findRegion("btn_normal"))
        val click = TextureRegionDrawable(textureAtlas.findRegion("btn_click"))
        ImageButton(normal, click)
    }
    // 右边按钮
    private val rightBtn by lazy {
        val normal = TextureRegionDrawable(textureAtlas.findRegion("btn_normal").copy().flipX())
        val click = TextureRegionDrawable(textureAtlas.findRegion("btn_click").copy().flipX())
        ImageButton(normal, click)
    }
    // 马里奥对象
    private val mario by lazy { Mario(0f, 0f, textureAtlas) }
    // 商店按钮
    private val shopBtn by lazy { Image(textureAtlas.findRegion("shop_btn")) }


    // 地图
    private val map by lazy {
        TmxMapLoader().load("map2.tmx")
    }

    // 渲染器
    private val otRender by lazy {
        OrthogonalTiledMapRenderer(map)
    }


    init
    {

        textureAtlas.findRegions("")


        shopBtn.translate(Gdx.graphics.width - shopBtn.width - 10, Gdx.graphics.height - shopBtn.height - 10)
        shopBtn.setOnTouchListener { _, touchState ->
            if (touchState == TouchState.touchUp)
            {
                stageFlag = MainPage.STAGE_FLAG.STORE
            }
        }

        rightBtn.setPosition(64f, 0f)
        leftBtn.setOnTouchListener { _, touchState ->
            when (touchState)
            {
                TouchState.touchDown ->
                {
                    mario.state = Mario.STATE.LEFT
                }
                TouchState.touchUp ->
                {
                    mario.state = Mario.STATE.LEFT_IDLE
                }
            }
        }
        rightBtn.setOnTouchListener { _, touchState ->
            when (touchState)
            {
                TouchState.touchDown ->
                {
                    mario.state = Mario.STATE.RIGHT
                }
                TouchState.touchUp ->
                {
                    mario.state = Mario.STATE.RIGHT_IDLE
                }
            }
        }

        // this.addActor(bgImg)
        this.addActor(leftBtn)
        this.addActor(rightBtn)
        this.addActor(shopBtn)

        map.layers.forEach { mapLayer ->
            mapLayer.objects.forEach { mapobject ->
                val obj = mapobject as RectangleMapObject
                if ("mario" == obj.name)
                {
                    mario.x = obj.rectangle.x
                    mario.y = obj.rectangle.y
                }
                if ("glod" == obj.name)
                {
                    val goldImg = Image(goldTexture)
                    goldImg.x = obj.rectangle.x
                    goldImg.y = obj.rectangle.y
                    this.addActor(goldImg)
                }
            }
        }

        this.addActor(mario)
    }


    override fun draw()
    {
        otRender.setView(camera as OrthographicCamera)
        otRender.render()
        super.draw()
    }


}